# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:  # 获取江淮车型层级列表

from MysqlTools import MysqlTools
import requests
import json
import os
import hashlib

class GetCarsInfo:

    def __init__(self, **kwargs):
        self.db = MysqlTools(db_config=kwargs.get("db_config"), type='dict', auto_commit=100)
        self.base_path = '/Users/duanmengdi/Desktop/workspace/py3_code/JAC_html/cars_info/'  # 文件保存的路径
        self.insert_table = kwargs.get("insert_table")

        self.base_url = 'http://218.104.65.183:8080/Ext/data/SpareInfo.ashx'
        self.cookies = {
            "ASP.NET_SessionId": "lxayqhhskmsgh51be30j04zj",
            "languageS": "China",
            "Userlanguages": "China",
            "security_session_verify": "a96df5b7be177b9d8d47fea60551cee1",
            # "": "",
        }
        self.headers = {
            "Origin": "http://218.104.65.183:8080",
            "X-Requested-With": "XMLHttpRequest",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36",
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
            "Referer": "http://218.104.65.183:8080/Ext/MainPage.aspx",
            # "": "",
            # "": "",
        }

        self.GetCarType_form = {
            "type": "",
            "key": "",
        }

    def get_md5(self, str):
        '''获取md5加密电子指纹'''
        md5 = hashlib.md5()
        md5.update(str)
        return md5.hexdigest()

    def parse_url(self, url, method, post_data=None, headers=None, tofile=True, **kwargs):
        requests.packages.urllib3.disable_warnings()  # 因为把ssl安全验证关了,所以不让代码报不安全的https请求警告
        key = kwargs.get("key", "")
        charset = kwargs.get("charset", "utf8")  # 暂时没有用到
        allow_redirects = kwargs.get("allow_redirects", False)  # 是否允许重定向, 默认不重定向,获取原始响应
        if method == 'GET':
            data = "GET" + url + key  # 拼接data
            md5 = self.get_md5(data)  # 获取此次请求电子指纹
            file_path = self.base_path + md5 + ".html"
            if os.path.exists(file_path):  # 判断文件是否存在,如果存在就从本地读取
                print("GET for local")
                with open(file_path, 'r') as f:
                    res = f.read()
                response = res
            else:  # 如果路径不存在, 从web端请求
                print("GET for web")
                try:
                    response = requests.get(url=url, verify=False, headers=headers, cookies=self.cookies, timeout=30,
                                            allow_redirects=allow_redirects)
                except Exception as e:
                    response = requests.get(url=url, verify=False, headers=headers, cookies=self.cookies, timeout=30,
                                            allow_redirects=allow_redirects)

                # 每次请求都获取cookies, 并更新self.cookies(如果获取到cookies的话)
                cookies = response.cookies.get_dict()
                for k in cookies:
                    self.cookies[k] = cookies[k]

                # 获取location,
                location = response.headers.get("Location", None)
                if location:
                    print("响应头中存在location")
                    self.location = location
                    print(location)
                    # return response.content.decode('utf8')

                if response:
                    if tofile:
                        with open(file_path, 'w') as f:  # 保存此次请求的html
                            f.write(response.content.decode('utf8'))
                    response = response.content.decode("utf8")
                else:
                    response = None

        elif method == "POST":
            data = "POST" + url + json.dumps(post_data)
            md5 = self.get_md5(data.encode())
            file_path = self.base_path + md5 + ".html"
            if os.path.exists(file_path):  # 判断文件是否存在,如果存在就从本地读取
                print("POST for local")
                with open(file_path, 'r') as f:
                    res = f.read()
                response = res
            else:
                print("POST for web")
                try:
                    response = requests.post(url=url, data=post_data, verify=False, headers=headers,
                                             cookies=self.cookies, timeout=30,
                                             allow_redirects=allow_redirects)
                except Exception as e:
                    response = requests.post(url=url, data=post_data, verify=False, headers=headers,
                                             cookies=self.cookies, timeout=30,
                                             allow_redirects=allow_redirects)

                # 获取cookies, 并更新self.cookies
                cookies = response.cookies.get_dict()
                for k in cookies:
                    self.cookies[k] = cookies[k]

                # 获取location,
                location = response.headers.get("Location", None)
                if location:
                    print("响应头中存在location")
                    self.location = location
                    print(location)
                    # response, file_path = self.parse_url(url=location, method="GET", headers=headers, local_pid=key)
                    # print "已重新请求location地址"
                    # return response, file_path
                    # return response.content.decode('utf8')

                if response:
                    if tofile:
                        with open(file_path, 'w') as f:  # 保存此次请求的html
                            f.write(response.content.decode('utf8'))
                    response = response.content.decode("utf8")
                else:
                    response = None
        return response, file_path

    def get_car_type_req(self, key):
        self.GetCarType_form["key"] = key
        self.GetCarType_form["type"] = "GetCarType"

        try:
            response, file_path = self.parse_url(url=self.base_url, method="POST", post_data=self.GetCarType_form,
                                                   headers=self.headers)
        except:
            response, file_path = self.parse_url(url=self.base_url, method="POST", post_data=self.GetCarType_form,
                                                   headers=self.headers)
        # return response, file_path
        car_type_list = response.split("#")
        _l = []
        for car in car_type_list:
            car_info_list = car.split("@")
            _dict = {
                "key":car_info_list[0],
                "name":car_info_list[1],
                "level":car_info_list[2],
            }
            _l.append(_dict)
            print(_dict)
        return _l, file_path

    def get_cid_info(self, key):
        self.GetCarType_form["key"] = key
        self.GetCarType_form["type"] = "LoadTreeNodes"
        try:
            response, file_path = self.parse_url(url=self.base_url, method="POST", post_data=self.GetCarType_form,
                                                 headers=self.headers)
        except:
            response, file_path = self.parse_url(url=self.base_url, method="POST", post_data=self.GetCarType_form,
                                                 headers=self.headers)

        if not response:
            yield False, False, False
        else:
            list1 = response.split("#")
            for i in list1:
                i = i.split("@")
                cid = i[0]
                name = i[2]
                factory = i[6]
                yield cid, name, factory


    def save2db(self, type, vehicle, years, version, cid, label, factory):
        str = type+'jac'+vehicle+years+version+label+factory
        md5 = self.get_md5(str.encode())
        _dict = dict(
            uid=md5,
            name=type,
            brand='jac',
            brandCode='jac',
            condition=vehicle,
            limit=years,
            limit2=version,
            limit3=label,
            label='',
            code=cid+'_'+factory,
            description='',
            imagePath='',
            imgName='',
            extra_info='',
            updatetime='now()',
            createtime='now()',
        )
        sql_tail = ''' on duplicate key update `brand`=values(`brand`)'''
        in_sql = self.db.get_insert_sql(self.insert_table, _dict)+sql_tail
        try:
            self.db.execute_sql(in_sql, commit=True)
        except Exception as e:
            print(in_sql)
            raise e
    def run(self):
        # level1
        car_type_list, file_path1 = self.get_car_type_req(key="1")
        for car_type in car_type_list:  # 车型  商务车,轿车等等
            key1 = car_type.get("key")
            name1 = car_type.get("name")
            level1 = car_type.get("level")
            Vehicle_list, file_path2 = self.get_car_type_req(key=key1)
            for vehile in Vehicle_list:  # 车系  瑞风M6,瑞风M5等等
                key2 = vehile.get("key")
                name2 = vehile.get("name")
                level2 = vehile.get("level")
                years_list, file_path3 = self.get_car_type_req(key=key2)
                for years in years_list:
                    key3 = years.get("key")
                    name3 = years.get("name")
                    level3 = years.get("level")
                    # print(key3, name3, level3)
                    if level3 == "4":
                        # cid, name5, factory = self.get_cid_info(key=key3)
                        for cid, name5, factory in self.get_cid_info(key=key3):
                            if not cid:
                                continue
                            _dict = self.save2db(type=name1, vehicle=name2, years="-",version=name3, cid=cid, label=name5, factory=factory)
                    if level3 == "3":
                        version_list, filepath4 = self.get_car_type_req(key=key3)
                        for version in version_list:
                            key4 = version.get("key")
                            name4 = version.get("name")
                            level4 = version.get("level")
                            if level4 == "4":
                                # cid, name5, factory = self.get_cid_info(key=key4)
                                for cid, name5, factory in self.get_cid_info(key=key4):
                                    if not cid:
                                        continue
                                    _dict = self.save2db(type=name1, vehicle=name2, years=name3, version=name4, cid=cid, label=name5, factory=factory)


if __name__ == '__main__':
    db_config = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="JAC",
        charset="utf8",
    )
    insert_table = 'cars_info_collection'
    tools = GetCarsInfo(db_config=db_config, insert_table=insert_table)
    tools.run()